package com.ghd.dataviz.controller;

import com.ghd.dataviz.core.param.DatavizResponse;
import com.ghd.dataviz.core.param.PageInfo;
import com.ghd.dataviz.entity.SyncPageInfo;
import com.ghd.dataviz.param.PageParam;
import com.ghd.dataviz.param.SyncPageParam;
import com.ghd.dataviz.service.ISyncPageInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ：ghd
 * @date ：Created in 2020-03-22 20:55
 * @description：动态页面管理相关接口
 * @modified By：
 * @version: $
 */
@Slf4j
@RestController
@Api(tags = "动态页面管理相关接口")
@RequestMapping("page")
public class PageController {

    private final ISyncPageInfoService pageInfoService;

    public PageController(ISyncPageInfoService pageInfoService) {
        this.pageInfoService = pageInfoService;
    }

    @PostMapping("add")
    @ApiOperation("添加动态页面")
    public DatavizResponse<SyncPageInfo> addPage(@RequestBody PageParam pageParam) {
        SyncPageInfo syncPageInfo = SyncPageInfo.builder()
                .name(pageParam.getName())
                .code(pageParam.getCode())
                .type(1)
                .fileId(pageParam.getFileId())
                .status(pageParam.getStatus())
                .build();
        syncPageInfo = pageInfoService.insertPageInfo(syncPageInfo);
        return DatavizResponse.success("添加页面成功！", syncPageInfo);
    }

    @PostMapping("list-sync-page")
    @ApiOperation("获取动态页面列表(分页)")
    public DatavizResponse<PageInfo<SyncPageInfo>> listSyncPage(@RequestBody SyncPageParam pageParam) {
        return DatavizResponse.success(pageInfoService.findSyncPageList(pageParam));
    }

    @PostMapping("edit")
    @ApiOperation("修改动态页面")
    public DatavizResponse<Void> editPage(@RequestBody PageParam pageParam) {
        SyncPageInfo syncPageInfo = SyncPageInfo.builder()
                .id(pageParam.getId())
                .name(pageParam.getName())
                .type(1)
                .status(pageParam.getStatus())
                .build();
        pageInfoService.editPage(syncPageInfo);
        return DatavizResponse.success("修改页面成功！");
    }

    @PostMapping("list-display-page")
    @ApiOperation("获取可展示页面列表")
    public DatavizResponse<List<SyncPageInfo>> listDisplayPage() {
        return DatavizResponse.success(pageInfoService.findDisplayPage());
    }
}
